<template>
  <a-drawer v-model:visible="LAYER.visible" :title="LAYER.title" :width="LAYER.width"
            :closable="LAYER.closable" :mask-closable="LAYER.maskClosable" :esc-to-close="LAYER.escToClose"
            :unmount-on-close="LAYER.unmountOnClose" :footer="LAYER.footer" :ok-text="LAYER.okText"
            :ok-loading="LAYER.okLoading" :cancel-text="LAYER.cancelText" :on-before-ok="onSubmit" @close="onCancel">
    <!--表单开始-->

    <a-form ref="formRef" :model="formState" layout="vertical" :rules="RULES">
      <a-row :gutter="[12,12]">
        <a-col :span="12">


          <a-form-item label="上级模块/分组" name="parentId">
            <smart-tree-selector v-model="formState.parentId" :url="SystemOptions.MenuTreeOptions" placeholder="请选上级模块/分组"/>
          </a-form-item>
        </a-col>


        <a-col :span="12">
          <a-form-item label="目录标题">
            <a-input v-model="formState.meta.title" placeholder="请填写目录标题"/>
          </a-form-item>
        </a-col>


        <a-col :span="12">
          <a-form-item label="目录路径" field="path">
            <a-input v-model.trim="formState.path" placeholder="请填写目录路径"/>
          </a-form-item>
        </a-col>

        <a-col :span="12">
          <a-form-item label="目录图标">
            <smart-icon-picker v-model="formState.meta.icon" placeholder="请填写模块图标"/>
          </a-form-item>
        </a-col>


        <a-col :span="12">
          <a-form-item label="展现样式">
            <smart-bool v-model="formState.meta.groupMode" true-text="分组模式" false-text="折叠模式"/>
          </a-form-item>
        </a-col>

        <a-col :span="12">
          <a-form-item :label="`显示排序值 [ ${formState.sort} ]` " field="sort"
                       tooltip="升序排序 显示值越小 显示越靠前">
            <a-slider v-model="formState.sort"/>
          </a-form-item>

        </a-col>
      </a-row>

    </a-form>
    <!--表单结束-->
  </a-drawer>
</template>

<script lang="ts" setup>

import {EditDrawer} from '@/libs/core'
import {SystemOptions} from '@/libs/options'
import {MENU_EDIT} from "../resource/form";
import {SystemHttp} from '../resource/http'

const props = defineProps({record: {type: Object, required: true}})
const emit = defineEmits(['cancel'])
const {BODY, RULES} = MENU_EDIT

const {addMenu, updateMenu} = SystemHttp
const config = {tplRecord: BODY, rawRecord: props.record, rowKey: 'id', emit}

const service = new EditDrawer(config).setAddRequest(addMenu).setUpdateRequest(updateMenu)
const {formRef, formState, LAYER, isUpdate, onCancel, onSubmit} = service
LAYER.title = isUpdate ? '新建分组' : '修改分组'
service.afterValidateHandler = () => {
  formState.value.component = 'LAYOUT'
  formState.value.name = formState.value.path
  formState.value.attribute = {value: "GROUP"}
  return true
}

</script>
